<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Simple Code Editor</title>
    <style>
        /* 编辑器容器的样式 */
        #editor {
            width: 100%;
            height: 400px;
            border: 1px solid #ccc;
            font-family: monospace;
            white-space: pre;
            background: #f9f9f9;
            overflow: auto;
            padding: 10px;
            margin-top: 20px;
        }

        /* 按钮的样式 */
        button {
            margin: 5px;
        }

        /* 模式选择器样式 */
        select {
            margin: 5px;
        }

        /* 基本代码高亮样式 */
        .keyword {
            color: blue;
        }

        .string {
            color: green;
        }

        .comment {
            color: grey;
        }
    </style>
</head>
<body>
    <!-- 语言模式选择器 -->
    <select id="modeSelect">
        <option value="javascript">JavaScript</option>
        <option value="html">HTML</option>
        <option value="css">CSS</option>
    </select>
    <!-- 编辑器容器 -->
    <div id="editor" contenteditable="true">// Start coding here</div>
    <!-- 保存按钮 -->
    <button id="saveButton">Save Content</button>
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const editor = document.getElementById('editor');
            const saveButton = document.getElementById('saveButton');
            const modeSelect = document.getElementById('modeSelect');

            // 保存内容到本地文件
            saveButton.addEventListener('click', function() {
                const content = editor.innerText;
                saveToFile(content);
            });

            // 切换语言模式
            modeSelect.addEventListener('change', function() {
                const mode = modeSelect.value;
                highlightSyntax(editor, mode);
            });

            // 文本内容变化时触发高亮
            editor.addEventListener('input', function() {
                const mode = modeSelect.value;
                highlightSyntax(editor, mode);
            });

            // 保存内容到本地文件的函数
            function saveToFile(content) {
                const blob = new Blob([content], { type: 'text/plain' });
                const link = document.createElement('a');
                link.href = URL.createObjectURL(blob);
                link.download = 'code.txt';
                link.click();
            }

            // 高亮语法的函数
            function highlightSyntax(editor, mode) {
                const text = editor.innerText;
                let highlightedText;

                if (mode === 'javascript') {
                    highlightedText = highlightJavaScript(text);
                } else if (mode === 'html') {
                    highlightedText = highlightHTML(text);
                } else if (mode === 'css') {
                    highlightedText = highlightCSS(text);
                } else {
                    highlightedText = text;
                }

                editor.innerHTML = highlightedText;
            }

            // 高亮 JavaScript 代码
            function highlightJavaScript(text) {
                return text
                    .replace(/(\/\/.*$)/gm, '<span class="comment">$1</span>')
                    .replace(/(\bfunction\b|\bvar\b|\bconst\b|\blet\b|\bif\b|\belse\b|\breturn\b|\bfor\b|\bwhile\b|\bdo\b|\bbreak\b|\bcontinue\b|\bnew\b|\btry\b|\bcatch\b|\bfinally\b|\bthrow\b|\bswitch\b|\bcase\b|\bdefault\b)/g, '<span class="keyword">$1</span>')
                    .replace(/(\".*?\"|\'.*?\')/g, '<span class="string">$1</span>');
            }

            // 高亮 HTML 代码
            function highlightHTML(text) {
                return text
                    .replace(/(&lt;!--.*?--&gt;)/g, '<span class="comment">$1</span>')
                    .replace(/(&lt;[a-z]+&gt;)/g, '<span class="keyword">$1</span>')
                    .replace(/(\".*?\"|\'.*?\')/g, '<span class="string">$1</span>');
            }

            // 高亮 CSS 代码
            function highlightCSS(text) {
                return text
                    .replace(/(\/\*.*?\*\/)/g, '<span class="comment">$1</span>')
                    .replace(/(\bcolor\b|\bbackground\b|\bmargin\b|\bpadding\b|\bborder\b|\bwidth\b|\bheight\b|\bfont\b|\btext\b|\balign\b|\bdisplay\b|\bflex\b|\bgrid\b|\bposition\b|\btop\b|\bbottom\b|\bleft\b|\bright\b|\bz-index\b)/g, '<span class="keyword">$1</span>')
                    .replace(/(\".*?\"|\'.*?\')/g, '<span class="string">$1</span>');
            }

            // 初始化高亮
            highlightSyntax(editor, modeSelect.value);
        });

    </script>
</body>
</html>
